import Mock from 'mockjs' // 导入 mockjs
import {useSysStudentAnswerSheetStore} from '../stores/studentAnswerSheet.js';
import {useSysClassStore} from "../stores/class.js";
import {storeToRefs} from "pinia";


const store = useSysStudentAnswerSheetStore();

// 查询学生试卷
Mock.mock("/api/query/student/paper", 'post', (options) => {
    const queryData = JSON.parse(options.body);
    if (queryData.state === 1000){
        try {
            const data = store.getAllSheets;
            console.log('Mock 获取的查询结果:', data)
            return {
                code: 200,
                msg: '查询成功',
                data: data,
            };
        }
        catch (e) {
            return {
                code: 500,
                msg: '查询失败',
                data: e,
            };
        }
    }
    try {
        const { findPapersByConditions  } = storeToRefs(store);
        // 调用 getter，它会返回一个函数
        const findFunction = findPapersByConditions .value;
        // 执行这个函数并传入查询条件
        const filteredPapers = findFunction(queryData);
        console.log('Mock 获取的查询结果:', filteredPapers)
        return {
            code: 200,
            msg: '查询成功',
            data: filteredPapers,
        };
    }
    catch (e) {
        return {
            code: 500,
            msg: '查询失败',
            data: e,
        };
    }
})

// 修改学生试卷
Mock.mock("/api/update/student/paper", 'put', (options) => {
    let updateData = JSON.parse(options.body);
    if (updateData.state !== 4){
        updateData = {
            ...updateData,
            state: 3,
        }
    }
    console.log('Mock 获取的修改数据:', updateData)
    try {
        const newData = store.updateSheet(updateData);
        if (newData === 1){
            return {
                code: 200,
                msg: '修改成功',
                data: newData,
            };
        }else {
            return {
                code: 500,
                msg: '修改失败',
                data: 0,
            };
        }

    }
    catch (e) {
        return {
            code: 500,
            msg: '修改失败',
            data: e,
        };
    }
})

// 新增学生试卷
Mock.mock("/api/save/new/student/paper", 'post', (options) => {
    const saveData = JSON.parse(options.body);
    try {
        const newData = store.addSheet(saveData);
        return {
            code: 200,
            msg: '修改成功',
            data: newData,
        };
    }
    catch (e) {
        return {
            code: 500,
            msg: '修改失败',
            data: e,
        };
    }
})

// 删除学生试卷
Mock.mock(/\/api\/delete\/student\/paper\/\w+/, 'delete', (options) => {
    // id数组
    const idList = options.url.split('/').pop().split(',');
    console.log('准备删除的ID列表:', idList);

    try {
        idList.forEach(id => {
            const isd = store.deleteSheet(id);
            if (isd === 0){
                return {
                    code: 500,
                    msg: `${id}删除失败`,
                    data: 0,
                }
            }
        })
        return {
            code: 200,
            msg: '修改成功',
            data: null,
        }
    }
    catch (e) {
        return {
            code: 500,
            msg: '修改失败',
            data: e,
        };
    }
})

